import * as usingService from '../../services/using';
import {queryAssetType} from '../../services/assetType';
import {qryDepts,qryAllUser} from '../../services/user';
import {queryAssetBrand,queryAssetModelAll} from '../../services/assetModel';
import {queryAllRoomList} from '../../services/tRoom';
import {querAssetListByParam} from '../../services/equipment';

export default {
  namespace: 'distribution',
  state: {
    list: [],
    assetTypeTree:[],
    deptTree:[],
    roomList:[],
    assetBrandList:[],
    userList:[],
    assetList:[],
    current : 1,
    currentSize : 10,
    total : 0, 
    modalVisible : false,
    modalType : 'create',
    newKey : '',
    detailList:[],
    currentItem: {},
    viewModalVisible:false,
    assetList:[],
    viewItem:{},
    viewDetailList:[],
    assetCurrent: 1,
    assetCurrentSize: 10,
    assetTotal: 0,
    secondModalVisible:false,

  },
  reducers: {
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    queryListByParamSuc(state, { payload }) {
      return { ...state, ...payload };
    },    
    changeDetailListSuc(state, { payload }) {
      return { ...state, ...{detailList:payload,assetList:[]} };
    },
    qryDeptsSuc(state, { payload: deptTree }) {
      return { ...state, deptTree };
    },
    queryBrandListSuc(state, { payload: assetBrandList }) {      
      return { ...state, assetBrandList };
    },
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },
    queryUserSuc(state, { payload:userList}) {
      return { ...state, userList };
    },
    queryAssetTypesSuc(state, { payload: assetTypeTree }) {
      return { ...state, assetTypeTree };
    },
    showModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
      return { ...state, modalVisible: false, detailList:[], newKey : new Date().getTime() + '' }
    },

    showSecondModal (state, { payload }) {
      return { ...state, ...payload,  secondModalVisible: true }
    },
    hideSecondModal (state) {
      return { ...state, assetList:[],assetCurrent:1,assetTotal:0, secondModalVisible: false }
    },

    showViewModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , viewModalVisible: true }
    },
    hideViewModal (state) {
      return { ...state, viewModalVisible: false,newKey : new Date().getTime() + '' }
    },
  },
  effects: {    
    *qryDepts({ payload },{ call, put }) {    
      const { body }  = yield call(qryDepts);
      yield put({ type: 'qryDeptsSuc', payload: body.data });
    },
    *queryUser({ payload },{ call, put }) {    
      const { body }  = yield call(qryAllUser);
      yield put({ type: 'queryUserSuc', payload: body.data });
    },
    *queryAssetType({ payload },{ call, put }) {
      const { body }  = yield call(queryAssetType);
      yield put({ type: 'queryAssetTypesSuc', payload: body.data });
    },
    *queryRoom({ payload },{ call, put }) {
      const { body } = yield call(queryAllRoomList,payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    },
    *queryAssetBrand({ payload },{ call, put }) {
      const { body } = yield call(queryAssetBrand,payload);
      yield put({ type: 'queryBrandListSuc', payload: body.data });
    }, 

    *queryList({ payload },{ call, put }) {
      const { body } = yield call(usingService.queryList,payload);
      yield put({ type: 'queryListSuc', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords}});
    },
    *qryById({ payload },{ call, put }){
      const { body } = yield call(usingService.qryById,payload);
      yield put({
          type: 'showModal',
          payload: {
            currentItem: body.data,
          },
      });
        yield put({ type: 'queryAssetBrand', payload: {  },});
        yield put({ type: 'queryRoom', payload: {  },});
    },

    *onViewItem({ payload },{ call, put }){
      const { body } = yield call(usingService.qryById,payload);
      yield put({type: 'showViewModal', payload: {viewItem: body.data, viewDetailList:body.dataVo},
      });
    },

    *changeDetailList({ payload },{ call, put }){
       yield put({type: 'hideSecondModal'});
       yield put({type: 'changeDetailListSuc', payload});
    },

    *showSecondAndAsset({ payload },{ call, put }){
      yield put({type: 'showSecondModal'});
      yield put({type: 'querAssetListByParam', payload});
   },

    *querAssetListByParam({ payload },{ call, put }){
      const { body } = yield call(querAssetListByParam,payload);
      yield put({ type: 'queryListByParamSuc', payload: { assetList:body.data, assetCurrent: body.current, assetCurrentSize: body.size, assetTotal: body.iTotalRecords}});
    },
    *doDistribution({ payload },{ call, put }){
      const { body } = yield call(usingService.doDistribution,payload);
      yield put({ type: 'hideModal' });
      yield put({ type: 'reload' });
    },
    *edit({ payload },{ call, put }){
      const { body } = yield call(usingService.edit,payload);
      yield put({ type: 'reload' });
    },
    *remove({ payload },{ call, put }){
      const { body } = yield call(usingService.remove,payload.id);
      yield put({ type: 'queryList', payload:payload.search });
    },
    *reload(action, { put }) {
      yield put({ type: 'queryList', payload:{ 'name':'' }});
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/using/distribution') {
          dispatch({ type: 'qryDepts' });
          dispatch({ type: 'queryAssetType' });
          dispatch({ type: 'queryUser' });
          dispatch({ type: 'queryList', payload:{ 'status':'' }});
        }
      });
    },
  },
};
